import { hasAuth } from "@/router/utils";
import { dealDataExcept } from "@/utils/common";
import { goodsCodeRend, goodsNameOverRend } from "@/utils/tableColumns";
import type { PaginationProps } from "@pureadmin/table";
import dayjs from "dayjs";
import { onMounted, reactive, ref } from "vue";

// api
import { getStoreSimplePage } from "@/api/basicArchives/storeManagement"; // 门店
import { getSupplierSimplePage } from "@/api/basicArchives/supplierManagement"; // 获取供应商名称
import { getInventoryList } from "@/api/stock/inventory";

export function useExrule() {
  const form = reactive({
    storeId: "",
    supplierId: "",
    businessDate: [],
    businessDateStart: "",
    businessDateEnd: "",
    pageNum: 1,
    pageSize: 20
  });

  const selectData = ref({
    storeList: [],
    supplierList: []
  });

  const loading = ref(false);
  const dataList = ref([]);
  const fileList = ref();

  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 20,
    currentPage: 1,
    background: true,
    pageSizes: [20, 50, 100]
  });

  const columns: TableColumnList = [
    {
      label: "业务日期",
      prop: "businessDate",
      fixed: "left",
      minWidth: "120px",
      formatter: ({ businessDate }) => dayjs(businessDate).format("YYYY-MM-DD")
    },
    // {
    //   label: "门店名称",
    //   prop: "storeName",
    //   minWidth: "120px"
    // },
    {
      label: "供应商名称",
      prop: "supplierName",
      minWidth: "120px"
    },
    {
      label: "业务单号",
      prop: "storeOrderCode",
      minWidth: "150px"
    },
    {
      label: "商品名称/规格名称",
      prop: "des",
      minWidth: "180px",
      cellRenderer: goodsNameOverRend
    },
    {
      label: "商品编码/规格编码",
      prop: "des",
      minWidth: "120px",
      cellRenderer: goodsCodeRend
    },
    {
      label: "商品数量",
      prop: "putInNum",
      minWidth: "120px"
    },
    {
      label: "单价",
      prop: "avgBuyPrice",
      minWidth: "120px",
      formatter: ({ avgBuyPrice }) => dealDataExcept(avgBuyPrice)
    },
    {
      label: "金额",
      prop: "putInPrice",
      minWidth: "120px",
      formatter: ({ putInPrice }) => dealDataExcept(putInPrice)
    }
  ];

  function resetForm(formEl) {
    if (!formEl) return;
    formEl.resetFields();
    onSearch();
  }

  async function onSearch() {
    loading.value = true;

    // 处理请求参数
    let tempForm = { ...form };
    if (tempForm.businessDate && tempForm.businessDate.length > 0) {
      tempForm.businessDateStart = tempForm.businessDate[0];
      tempForm.businessDateEnd = tempForm.businessDate[1];
    }
    delete tempForm.businessDate;

    const { data } = await getInventoryList(tempForm);
    dataList.value = data.records;
    pagination.total = data.total;
    pagination.pageSize = data.pageSize;
    pagination.currentPage = data.pageNum;

    setTimeout(() => {
      loading.value = false;
    }, 500);
  }

  function handleSizeChange(val: number) {
    form.pageSize = val;
    onSearch();
  }

  function handleCurrentChange(val: number) {
    form.pageNum = val;
    onSearch();
  }

  async function getCommon() {
    const params = {
      status: 0,
      pageNum: 1,
      pageSize: 999
    };
    // 供应商列表
    getSupplierSimplePage(params).then(res => {
      selectData.value.supplierList = res.data?.records || [];
    });

    // 门店
    getStoreSimplePage(params).then(res => {
      selectData.value.storeList = res.data?.records || [];
    });
  }

  onMounted(() => {
    onSearch();

    getCommon();
  });

  return {
    form,
    columns,
    loading,
    dataList,
    pagination,
    fileList,
    selectData,
    onSearch,
    handleSizeChange,
    handleCurrentChange,
    resetForm,
    hasAuth
  };
}
